package com.ucode.wap.hrm.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import com.ucode.hrm.service.HrmPersonalResumeService;
import com.ucode.model.hrm.HrmPersonalResume;
import com.ucode.model.hrm.convert.HrmPersonalResumeConvert;
import com.ucode.model.hrm.form.HrmPersonalResumeForm;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.IdentifyUtils;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Tag(name = "移动端 - 人力资源系统 - 个人简历")
@RestController
@RequestMapping("/hrm/personal-resume")
@Validated
public class ApiHrmPersonalResumeController {

    @Resource
    private HrmPersonalResumeService personalResumeService;
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @NeedLogin
    @GetMapping("/get")
    @Operation(summary = "获取个人简历信息")
    public ResponseResult<HrmPersonalResume> get(){
        HrmPersonalResume personalResume = personalResumeService.selectOneByMemberId(currentLoginUserHandler.getCurrentUserId());
        return ResponseResult.success(personalResume);
    }

    @NeedLogin
    @PostMapping("/create")
    @Operation(summary = "创建")
    public ResponseResult<Long> create(@Valid @RequestBody HrmPersonalResumeForm form) {
        HrmPersonalResume bean = HrmPersonalResumeConvert.INSTANCE.convert(form);
        bean.setId(IdentifyUtils.getDefaultSnowflakeId());
        bean.setMemberId(currentLoginUserHandler.getCurrentUserId());
        if(ObjUtil.isNotNull(bean.getBirthDate())) {
            bean.setAge(DateUtil.ageOfNow(LocalDateTimeUtil.format(bean.getBirthDate(), DatePattern.NORM_DATE_PATTERN)));
        }
        Long id = personalResumeService.insert(bean);
        return id != null ? ResponseResult.success(id) : ResponseResult.failed();
    }

    @NeedLogin
    @PostMapping("/update/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "修改")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
                                         @Valid @RequestBody HrmPersonalResumeForm form) {

        HrmPersonalResume dbBean = personalResumeService.validate(id);
        if(!ObjUtil.equals(dbBean.getMemberId(),currentLoginUserHandler.getCurrentUserId()))
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);

        HrmPersonalResume bean = HrmPersonalResumeConvert.INSTANCE.convert(form);
        bean.setId(id);
        bean.setMemberId(dbBean.getMemberId());
        if(ObjUtil.isNotNull(bean.getBirthDate())) {
            bean.setAge(DateUtil.ageOfNow(LocalDateTimeUtil.format(bean.getBirthDate(), DatePattern.NORM_DATE_PATTERN)));
        }

        int i = personalResumeService.update(bean);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }


}
